<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/easyUI/easyui.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/easyUI/easyui-lang-zh_CN.js" type="text/css"></link>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<%--修正上传问题用的插件--%>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.form.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/easyUI/jquery.easyui.min.js"></script>
   
<style>
input.textbox {
width: 135px;
}
</style>
 <form action="" method="post" id="uploadForm" class="easyui-form"> 
 <table border="1" style="width:600px;margin-bottom:200px">
	<thead>
		<tr>
			<th>序号</th>
			<th>文件名称</th>
			<th>文件格式</th>
			<th>操作</th>		
		</tr>
	</thead>
	<tbody class="tbody_data"></tbody>
</table>		
 
 
 <table>	
		<tr>  
          <td class='labely'>上传文件：</td>  
          <td class='context' colspan='3'>  
              <input type="file" id="imgId" name="imgId" class="text"> 
              <a id="ispresult_save" onclick="ImportPDF('imgId');"  href="javascript:void(0);" class="easyui-linkbutton">上传</a> 
          </td>  
      </tr> 
		<tr>  
          <td class='labely'>上传文件2：</td>  
          <td class='context' colspan='3'>  
              <input type="file" id="imgMore" name="imgMore" class="text"> 
              <a id="ispresult_save" onclick="ImportMorePDF('imgMore');"  href="javascript:void(0);" class="easyui-linkbutton">上传</a> 
          </td>  
      </tr> 
  </table>    
   </form>
<script type="text/javascript">
function ImportPDF(file_id){
	var pdfFile = $("#imgId").val();
	alert(pdfFile+"---"+file_id);
	if($.trim(pdfFile)==""){
		alert("请选择上传的文件");
		return false;
	}		
	var fileName = pdfFile.split("\\")[pdfFile.split("\\").length -1];
	//获取后缀名
	var lastName = pdfFile.split(".")[pdfFile.split(".").length -1];
	var lastNameTemp = lastName.toLowerCase();
	if(lastNameTemp != "pdf" && lastNameTemp != "jpg" && lastNameTemp != "mp3"){
		alert("文件格式错误");
		return false;
	}		
	 $.ajaxFileUpload({
        url: '${pageContext.request.contextPath}/upload.jspx', 
        secureuri: false, 
        fileElementId: file_id,
        dataType: 'json', 
        success: function (data){
           if(data.code==0){
        	   clearFileInput();//清空
        	  saveData(fileName,data.content,lastNameTemp);
           }
        },
        error: function (data, status, e){
            alert(e);
        }
    });	
}
function ImportMorePDF(imgMore){
	 var formData = new FormData();
     formData.append('imgMore', $('#imgMore')[0].files[0]);
		
     $.ajax({
         url:"${pageContext.request.contextPath}/uploadMore.jspx",
         type:'POST',
         dataType:'JSON',
         cache: false,
         data:formData,
         processData: false,
         contentType:false,
         success:function(data){
             //alert(JSON.stringify(data));
             if (data.code==0) {
                 alert("上传成功");
             } else {
                 console.log("导入失败");
                 return false;
             }
         },
         error:function(data){
  			alert("导入失败");
         }
     });
	
}
function clearFileInput() {
    var file = $("#imgId");
    file.after(file.clone().val(""));
    file.remove();
}
var data_array=[];
function saveData(fileName,url,fileType){
	$.ajax({
		url:'${pageContext.request.contextPath}/saveImport.jspx',
		data:{"fileName":fileName,"ImpUrl":url,"fileType":fileType},
		datType:'json',
		type:'post',
		success:function(data){
			if(data.code==0){
				console.log(data.content);
				var response_json = {"id":data.content.id,"fileName":data.content.fileName,"fileType":data.content.fileType};						
				data_array.push(response_json);
				show_data(); 					
				
			}else{
				console.log(data.message);
			}
		},
		error:function(data){
			console.log(data.message);
		}
	});
}

function show_data(){
	var temp_html = "";
	for(var item = 0;item < data_array.length;item++){
		temp_html +='<tr><td>'+(item+1)+'<input type="hidden" class="ids" value="'+data_array[item].id+'"></td>'+
		'<td>'+data_array[item].fileName+'</td><td>'+data_array[item].fileType+'</td><td><a href="javascript:" onclick="deleteData('+item+')">删除</a></td></tr>'
	}
	$(".tbody_data").html(temp_html);
}

function deleteData(index){
	data_array.splice(index,1);
	show_data();
}

</script>
